package com.sservice.web.controller.pc;

import com.sservice.base.SameUrlData;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.utils.DateUtil;
import com.sservice.common.Code;
import com.sservice.model.entity.Commission;
import com.sservice.model.vo.CommissionVo;
import com.sservice.service.service.ICommissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/commission")
public class CommissionController {

    @Autowired
    ICommissionService commissionService;

    /**
     * 创建
     *
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(Commission arg) {
        return new BaseResult(true, commissionService.save(arg));
    }

    /**
     * 分享领红包
     *
     * @param arg
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/shareGetRedEnvelope", method = RequestMethod.POST)
    public BaseResult<Object> redEnvelope(CommissionVo arg) {
        if (arg.getType1() == null) {
            return new BaseResult(false, Code.INCOMPLETE_PARAMETERS);//
        }
        arg.setType1(CommissionVo.generateType1(CommissionVo.DATA_TYPE1_RED_ENVELOPE, arg.getType1()));//
        Commission commission = commissionService.existsBy(arg);

        if (commission != null) {
            return new BaseResult(false, "您已经获得过此次分享的红包,不能再次领取", Code.GOT_RED_ENVELOPE);
        }
        arg.setDateStart(DateUtil.dateToStr(DateUtil.getFirstDateOfMonth(new Date())));
        arg.setDateEnd(DateUtil.dateToStr(DateUtil.getLastDateOfMonth(new Date())));
        Commission commission1 = commissionService.existsBy(arg);
        if (commission1 != null) {
            return new BaseResult(false, "1个月内只能领取1次,不能再次领取", Code.NOT_GOT_MORE_IN_TIME);
        }

        return new BaseResult(true, commissionService.save(arg));
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BaseResult<Object> list(CommissionVo arg) {
        return commissionService.userHistoryRecord(arg);
    }


}
